package com.cloudptt.wp.ui.message;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.MsgOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.more.FuncPreferenceActivity;
import com.cloudptt.wp.ui.more.ServerInfoActivity;
import com.cloudptt.wp.ui.more.VersionInfoActivity;
import com.mcptt.db.model.AddrInfo;

public class AddrInfoActivity extends Activity{

	private final static String TAG = "SettingsActivity";

	private ImageView backup_btn;
	private TextView user_num;
	private TextView user_name;
	//	private TextView versionVal;
	private Button exit_bt;
	private Button clearBtn;

	private SettingsAdapter mAdapter;
	private ListView mSettingsListView;

	private ImageView mUserIconImageView;

	//	private McIdInfo info = null;
	private AddrInfo peerAddrInfo;


	public static int[] titleArray = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(getResources().getColor(R.color.app_theme_color_primary));

		super.onCreate(savedInstanceState);
		setContentView(R.layout.addrinfo_activity);
		peerAddrInfo =(AddrInfo)getIntent().getSerializableExtra(SessionActivity.KEY);
		Log.i(TAG,"onCreate peerAddrInfo =  "+ peerAddrInfo);
		if(peerAddrInfo.isGroupType())
		{
			titleArray = new int[] {
					R.string.group_call_video
					,R.string.title_group_member
			};
		}
		else
		{
			titleArray = new int[] {
					R.string.single_call_voice
					,R.string.single_call_video
					,R.string.single_call_video_push
					,R.string.single_call_video_pull
			};
		}

		initView();
		//initData();

		ScreenManager.getScreenManager().pushActivity(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		mSettingsListView  = (ListView) findViewById(R.id.memberLv);
		mSettingsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				doFuncFormItemPos(position);
			}
		});
		mAdapter = new SettingsAdapter();
		mSettingsListView.setAdapter(mAdapter);

		clearBtn = (Button) findViewById(R.id.clearBtn);
		clearBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doClearSessionAction();
			}});

		backup_btn = (ImageView) findViewById(R.id.back_up_icon);
		backup_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ScreenManager.getScreenManager().popActivity();
				overridePendingTransition( R.anim.in_from_left, R.anim.out_to_right );
			}
		});

		user_num  = (TextView) findViewById(R.id.user_info_num);
		user_name = (TextView) findViewById(R.id.user_info_name);
		user_num.setText(peerAddrInfo.getAddrId());
		user_name.setText(peerAddrInfo.getDscp());
	}

	@Override
	protected void onDestroy(){
		// TODO Auto-generated method stub
		super.onDestroy();
	}



	private void doFuncFormItemPos(int pos){
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		Intent intent = new Intent();
		switch (pos) {
			case 0:
				if(peerAddrInfo.isGroupType()) {
					if(product.getGCallEventAdapter() != null){
						product.getGCallEventAdapter().onPtt(Const.MSG_TALK_ON, true,true,true,false);
					}
				}
				else
				{
					product.getPCallEventAdapter().makePrivateCall(peerAddrInfo, CallAttr.CallType_Private,false,false,"");
				}
				break;

			case 1:
				if(peerAddrInfo.isGroupType()) {
					intent.setClass(this, GroupMemberActivity.class);
					intent.getBooleanExtra(GroupMemberActivity.KEY_SHOW,false);
					intent.putExtra(SessionActivity.KEY,peerAddrInfo);
					//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
				else
				{
					product.getPCallEventAdapter().makePrivateCall(peerAddrInfo, CallAttr.CallType_Private,true,false,"");
				}
				break;

			case 2:
				if(peerAddrInfo.isGroupType()) {
					intent = new Intent();
					intent.setClass(this, FuncPreferenceActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
				else
				{
					product.getPCallEventAdapter().makePrivateCall(peerAddrInfo, CallAttr.CallType_Push_One_To_One,true,false,"");
				}
				break;

			case 3:
				if(peerAddrInfo.isGroupType()) {
					intent = new Intent();
					intent.setClass(this, FuncPreferenceActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
				else
				{
					product.getPCallEventAdapter().makePrivateCall(peerAddrInfo, CallAttr.CallType_Pull_One_To_One,true,false,"");
				}
				break;



			default:
				break;
		}
	}


	private void doClearSessionAction()
	{
		Log.d(TAG, "doClearSessionAction");
		final boolean isGroup = peerAddrInfo.isGroupType();
		String tipStr = getString(R.string.del_user_history_records);

		DialogInterface.OnClickListener okListener = new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				SysApplication app = ((SysApplication) getApplicationContext());
				Product product = app.getProduct();
				MsgOperator operator = (MsgOperator)product.getOperator(ProductConst.Func_Msg);
				String receiverUId = peerAddrInfo.getAddrId();//.getAddrInfo().getMcId();
				operator.delHirstory(receiverUId);//.delAllSession(receiverUId);
			}
		};

		DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{

			}
		};
		CommDlg.showConfirmDlg(this, tipStr, okListener, cancelListener);
	}


	class SettingsAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return titleArray.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return titleArray[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			ViewHolder viewHolder = new ViewHolder();

			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.settings_item_layout, parent, false);
				viewHolder.icon = (ImageView)convertView.findViewById(R.id.settings_item_icon);
				viewHolder.title = (TextView)convertView.findViewById(R.id.settings_item_title);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.icon.setImageResource(R.drawable.settings_about_us_icon);
			viewHolder.title.setText(titleArray[position]);

			return convertView;
		}

	}

	class ViewHolder{
		public ImageView icon;
		public TextView title;
	}

}
